<?php
class MyHelper
{
    public static function PriceToWord( $price, $culture = 'lt' )
    {
        // Ensure it is natural number
        $price = (int)$price;
        $word = '';
        $sPrice = '' . $price;

        // Check if irregular price, such as X11 or Y17
        $badPrice = $price % 100;
        switch(strlen($price))
        {
            case 1:
                $word = MyHelper::numberToWord($price, $culture);
                break;
            case 2:
                if( $price < 21 )
                {
                    $word = MyHelper::numberToWord($price, $culture);
                }else
                {
                    $word = MyHelper::decimalsToWord($sPrice[0], $culture);
                    $word .= ((int)$sPrice[1] == 0) ? '' : ' ' . MyHelper::numberToWord($sPrice[1], $culture);
                }

                //            $word .= ((int)$sPrice[1] == 0) ? '' : ' ' . numberToWord($sPrice[1], $culture);
                break;
            case 3:
                // No need to write 1 simtas
                $word = ((int)$sPrice[0] == 1) ? 'šimtas' : MyHelper::numberToWord($sPrice[0], $culture) . ' šimtai';

                if( $badPrice < 21 )
                {
                    $word .= ($badPrice == 0 ) ? '' : ' ' . MyHelper::numberToWord($badPrice, $culture);
                }else
                {
                    // Zerro is skiped in number like 105
                    if( (int)$sPrice[1] != 0 )
                    {
                        $word .= ' ' . MyHelper::decimalsToWord($sPrice[1], $culture);
                    }

                    // Do not display leading zerro
                    $word .= ((int)$sPrice[2] == 0) ? '' : ' ' . MyHelper::numberToWord($sPrice[2], $culture);
                }
                 
                //$word .= decimalsToWord($sPrice[1], $culture) . ' ' . numberToWord($sPrice[2], $culture);
                break;

            case 4:
                $word = MyHelper::numberToWord($sPrice[0], $culture) . ' tukst.';

                // No need to write 1 tuks
                $word.= ((int)$sPrice[1] == 1) ? '' : ' ' . MyHelper::numberToWord($sPrice[1], $culture) . ' šimtai';

                if( $badPrice < 21 )
                {
                    $word .= ($badPrice == 0 ) ? '' : ' ' . MyHelper::numberToWord($badPrice, $culture);
                }else
                {
                    // Zerro is skiped in number like 105
                    if( (int)$sPrice[2] != 0 )
                    {
                        $word .= ' ' . MyHelper::decimalsToWord($sPrice[2], $culture);
                    }

                    // Do not display leading zerro
                    $word .= ((int)$sPrice[3] == 0) ? '' : ' ' . MyHelper::numberToWord($sPrice[3], $culture);
                }
                break;

            default:
                throw new Exception('priceToWord does not support 5 digits numbers!!!');
                break;
        }

        return $word;
        //return ($price % 10 == 1) ? $word . ' LTL' : $word . ' LTL';
    }

    public static function NumberToWord( $n, $culture = 'lt' )
    {
        $arWords = array(
        'lt' => array(
        0 => 'nulis'
        , 1 => 'vienas'
        , 2 => 'du'
        , 3 => 'trys'
        , 4 => 'keturi'
        , 5 => 'penki'
        , 6 => 'šeši'
        , 7 => 'septyni'
        , 8 => 'aštuoni'
        , 9 => 'devyni'
        , 10 => 'dešimt'
        , 11 => 'vienuolika'
        , 12 => 'dvylika'
        , 13 => 'trylika'
        , 14 => 'keturiolika'
        , 15 => 'penkiolika'
        , 16 => 'šešiolika'
        , 17 => 'septiniolika'
        , 18 => 'aštuniolika'
        , 19 => 'devyniolika'
        , 20 => 'dvidešimt'
        )
        );

        return empty( $arWords[$culture] ) ? $n : empty($arWords[$culture][$n]) ? $n : $arWords[$culture][$n];
    }

    public static function decimalsToWord($n, $culture = 'lt')
    {
        $arWords = array(
        'lt' => array(
        0 => ''
        , 1 => 'desimt'
        , 2 => 'dvidešimt'
        , 3 => 'trisdešimt'
        , 4 => 'keturiasdešimt'
        , 5 => 'penkiasdešimt'
        , 6 => 'šešiasdešimt'
        , 7 => 'septyniasdešimt'
        , 8 => 'aštuoniasdešimt'
        , 9 => 'devyniasdešimt'
        )
        );

        return empty( $arWords[$culture] ) ? $n : empty($arWords[$culture][$n]) ? $n : $arWords[$culture][$n];
    }

    public static function hundredsToWord($n, $culture = 'lt')
    {
        $arWords = array(
        'lt' => array(
        0 => 'šimtas'
        , 1 => 'desimt'
        , 2 => 'dvidešimt'
        , 3 => 'trisdešimt'
        , 4 => 'keturiasdešimt'
        , 5 => 'penkiasdešimt'
        , 6 => 'šešiasdešimt'
        , 7 => 'septyniasdešimt'
        , 8 => 'aštuoniasdešimt'
        , 9 => 'devyniasdešimt'
        )
        );

        return empty( $arWords[$culture] ) ? $n : empty($arWords[$culture][$n]) ? $n : $arWords[$culture][$n];
    }

}
?>